package com.safone.thirdattendanceservices.classroom.services.impl;

import com.alibaba.fastjson.JSON;
import com.safone.thirdattendanceservices.classroom.services.ClassRoomAttendanceDataService;
import com.safone.thirdattendanceservices.configs.ArgsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author xy
 * @date 2020/12/4 20:47
 */
@Service
public class ClassRoomAttendanceDataServiceImpl implements ClassRoomAttendanceDataService{
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ArgsUtil argsUtil;

    private Boolean key = true;

    /**
     * 定时任务：
     *     每日晚12点获取当日考勤数据，并推送到kafka
     */
//    @Scheduled(cron = "55 59 23 * * ?")
//    @Scheduled(cron = "0/50 * * * * ? " )
    public void saveDormitoryAttendance() {
        if (key) {
            String prefixUrl =
                    "http://localhost:13000/api/safe/aggregation/externalData/pushDataDictionary";
            // 设置请求header信息
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.add("Accept", MediaType.ALL_VALUE);
            headers.add(
                    "Authorization",
                    "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MzMyIiwiY29kZSI6InNhZmUtc2Nob29sIiwiZXhwIjoxNjA4MTg5NTE2LCJpYXQiOjE2MDgxMDMxMTZ9.apf1zcfBqgR8uLYgdzt9yFoz4Jad2R6RW9rBzbszSNF0RGoM4P9GU5k5MXdy8ynA4rLZU_da29e9emS6K9s9yg");
            // 设置请求参数
            Map params = new HashMap<>();
            params.put("type", "classAttendance");
            String data =
                    "{\"dormitoryId\":\"宿舍楼id\",\"dormName\":\"宿舍楼名称\",\"date\":\"查询时间\",\"normalAttendance\":\"在寝室人数\",\"lateAttendance\":\"晚归人数\",\"totalAttendance\":\"寝室总人数\",\"notBackAttendance\":\"未归人数\"}";
            params.put("data", data);
            // 封装请求头和内容
            HttpEntity<Map> requestEntity = new HttpEntity<>(params, headers);
            // 发送请求
            ResponseEntity<String> stringResponseEntity =
                    restTemplate.postForEntity(prefixUrl, requestEntity, String.class);
            key = false;
        }
        //推送数据
        List<Map<String,Object>> idList = (List<Map<String,Object>>)getSchoolRoll().getBody().get("data");
        List<String> id = idList.stream().map(map -> (String)map.get("id")).collect(Collectors.toList());
        String ids = String.join(",", id);
        Map data = (Map)findClassAttendanceByDate(ids).getBody().get("data");
        List<Map<String,Object>> attendanceTotalList =(List<Map<String,Object>>) data.get("attendanceTotalList");

        String prefixUrl = "http://localhost:13000/api/safe/aggregation/externalData/pushData";
        // 设置请求header信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.ALL_VALUE);
        headers.add(
                "Authorization",
                "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MzMyIiwiY29kZSI6InNhZmUtc2Nob29sIiwiZXhwIjoxNjA4MTg5NTE2LCJpYXQiOjE2MDgxMDMxMTZ9.apf1zcfBqgR8uLYgdzt9yFoz4Jad2R6RW9rBzbszSNF0RGoM4P9GU5k5MXdy8ynA4rLZU_da29e9emS6K9s9yg");
        // 设置请求参数
        Map params = new HashMap();
        params.put("type", "dormitoryAttendance");
        params.put("typeDescribe", "宿舍楼考勤统计数据");
        params.put("data", JSON.toJSONString(attendanceTotalList));
        // 封装请求头和内容
        HttpEntity<Map> requestEntity = new HttpEntity<>(params, headers);
        // 发送请求
        ResponseEntity<String> stringResponseEntity =
                restTemplate.postForEntity(prefixUrl, requestEntity, String.class);
    }

    @Override
    public ResponseEntity<Map> findClassAttendanceByDate(String classIds) {
        String startDate="";
        String endDate="";
        String year="";
        String apiUrl2 = "/eags/api/fcs/v1.0/getAttendanceData"
                +"?classId="+classIds
                +"&startDate="+startDate
                +"&endDate="+endDate
                +"&year="+year;
        String httpMethod1 = "GET";
        argsUtil.restTemplateExchange(apiUrl2,httpMethod1,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<Map> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl2, HttpMethod.GET, formEntity, Map.class);
        return responseEntity;
        }

    @Override
    public Object findSchoolRollByName() {
        String name="";
        String apiUrl2 = "/eags/api/fsds/v1.0/school-roll/search/name"+"?name="+name;
        String httpMethod1 = "GET";
        argsUtil.restTemplateExchange(apiUrl2,httpMethod1,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl2, HttpMethod.GET, formEntity, String.class);
        return responseEntity;
    }

    @Override
    public Object findOrganization() {
        String name="";
        String apiUrl = "/eags/api/fcs/v1.0/organization?"
                +"name="+name;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
        return responseEntity;
    }

    @Override
    public Object findLesson() {
        Long orgId=1L;
        Integer pageNum=1;
        Integer pageSize=10;
        String keyword="";
        String apiUrl = "/eags/api/fcs/v1.0/lesson?"
                +"orgId="+orgId
                +"&pageNum="+pageNum
                +"&pageSize="+pageSize
                +"&keyword="+keyword;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
        return responseEntity;
    }

    @Override
    public Object findStudentBehaviorList() {
        String classIds="";
        String lessonIds="";
        String beginTime="";
        String endTime="";
        Integer lessonType=0;
        String apiUrl = "/eags/api/fcs/v1.0/getStudentBehaviorData?"
                +"classIds="+classIds
                +"&lessonIds="+lessonIds
                +"&beginTime="+beginTime
                +"&endTime="+endTime
                +"&lessonType="+lessonType;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
        return responseEntity;
    }

    @Override
    public Object findOneStudentBehavior() {
        Long studentId=1L;
        String beginTime="";
        String endTime="";
        Integer actionType=0;
        String apiUrl = "/eags/api/fcs/v1.0/behavior/list?"
                +"studentId="+studentId
                +"&beginTime="+beginTime
                +"&endTime="+endTime
                +"&actionType="+actionType;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
        return responseEntity;
    }

    @Override
    public ResponseEntity<Map> getSchoolRoll() {
        String id="";
        String name="";
        String pId="";
        int level=5;
        String apiUrl = "/eags/api/fcs/v1.0/school-rolls?"
                +"id="+id
                +"&name="+name
                +"&pid="+pId
                +"&level="+level;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<Map> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, Map.class);
        return responseEntity;
    }

}


